﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.DAO;
using AionCxxLibrary.Models;
using AionCxxLibrary.Enums;
using WorldServer.DAO;
using WorldServer.Data;
using System.Xml;
using WorldServer.Packets.Server;
using WorldServer.Data.Templates;
using WorldServer.Services;

namespace WorldServer.Models
{
    /// <summary>
    /// This interface must be implemented by any object that should hold an AionID.
    /// This AionID is in most cases given by the AionIDManager.
    /// </summary>
    public interface IAionIDAwareObject
    {
        int GetGUID();
    }

    /// <summary>
    /// Base class for any spawned object in AION.
    /// Holds an AionID and a Position.
    /// </summary>
    public abstract class AionObject : IAionIDAwareObject
    {
        int GUID;
        public Vector5D Position;

        public AionObject Target;

        public SeeList NearbyObjects;
        public StatsList StatsList;
        public LevelInstance LevelInstance;

        List<Delegate> MoveHandlers = new List<Delegate>();
        public delegate void ObjectMoveHandler(object sender, EventArgs<AionObject> args);
        public event ObjectMoveHandler Move;

        public AionObject(int guid)
        {
            GUID = guid;
            NearbyObjects = new SeeList(this);
        }

        public int GetGUID() { return GUID; }

        public abstract void SeeObject(AionObject obj);
        public abstract void UnseeObject(AionObject obj);

        public void OnMove()
        {
            if (Move != null)
            {
                Move(this, new EventArgs<AionObject>(this));
                Move = null;
            }
        }

        public void ClearEvents_Move()
        {
            lock (MoveHandlers)
            {
                foreach (ObjectMoveHandler omh in MoveHandlers)
                {
                    Move -= omh;
                }
                MoveHandlers.Clear();
            }
        }

        public void Pkt_Info(Character c)
        {
            if (this is Character)
            {
                Character ch = this as Character;
                if (!ch.Connected)
                    return;
                c.SendPacket(new SPkt_CharacterDetails(ch));
            }
            else if(this is NPC)
            {
                c.SendPacket(new SPkt_NPC(this as NPC));
            }
        }

        public void Pkt_Delete(Character c)
        {
            c.SendPacket(new SPkt_DespawnObject(GetGUID(), 100));
        }

    }

    /// <summary>
    /// Base class for all gameobjects.
    /// A gameobject is any AionObject except Players (NPCs, environment objects ...)
    /// </summary>
    public abstract class GameObject : AionObject
    {
        public GameObject() : base(GUIDManager.NextID())
        {
            StatsList = new StatsList(this);
        }
    }

    /// <summary>
    /// GameObject with a client NPC template.
    /// </summary>
    public class NPC : GameObject
    {
        public readonly int NpcID;
        public NPCTemplate Template;

        public AionObject Creator;

        public NPC(int NpcID)
        {
            this.NpcID = NpcID;
            Template = Datastore.NpcsById[NpcID];
        }

        public void Spawn(Vector5D Pos, LevelInstance Level)
        {
            Position = new Vector5D(Pos.WorldID, Pos.To4D());
            LevelInstance = Level;
            LevelService.AddObject(this);
            LevelInstance.Register(this);
            LevelInstance.RecalculateNearbyObjects(this);
        }

        public void Despawn()
        {
            LevelInstance.Unregister(GetGUID());
            LevelService.RemoveObject(this);
            LevelInstance = null;
            Position = null;
        }

        public override void SeeObject(AionObject obj)
        {
            
        }

        public override void UnseeObject(AionObject obj)
        {
            
        }
    }

}
